<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property string $id
 * @property string $product_category_id
 * @property string $title
 * @property string $quote
 * @property string $content
 * @property string $thumbnail
 * @property integer $status
 * @property integer $is_feature
 * @property integer $sorter_category
 * @property string $desc
 * @property string $date_created
 * @property string $date_edited
 *
 * The followings are the available model relations:
 * @property ProductCategory $productCategory
 * @property ProductImage[] $productImages
 */
class Product extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('product_category_id, title', 'required'),
			array('status, is_feature, sorter_category', 'numerical', 'integerOnly'=>true),
			array('product_category_id', 'length', 'max'=>10),
			array('title', 'length', 'max'=>200),
			array('quote, content, thumbnail, desc, date_edited', 'safe'),
			array('thumbnail', 'file',
					'types'=>'jpg,jpeg,png',
					'maxSize'=>1024 * 1024 * 5, // 1MB
					'tooLarge'=>'The file was larger than 5MB. Please upload a smaller file.',
					'allowEmpty'=>1,
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, product_category_id, title, quote, content, thumbnail, status, is_feature, sorter_category, desc, date_created, date_edited', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'productCategory' => array(self::BELONGS_TO, 'ProductCategory', 'product_category_id'),
			'productImages' => array(self::HAS_MANY, 'ProductImage', 'product_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'product_category_id' => 'Product Category',
			'title' => 'Title',
			'quote' => 'Quote',
			'content' => 'Content',
			'thumbnail' => 'Thumbnail',
			'status' => 'Status',
			'is_feature' => 'Is Feature',
			'sorter_category' => 'Sorter Category',
			'desc' => 'Desc',
			'date_created' => 'Date Created',
			'date_edited' => 'Date Edited',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->with = 'productCategory';
		$criteria->compare('id',$this->id,true);
		$criteria->compare('product_category_id',$this->product_category_id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('quote',$this->quote,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('thumbnail',$this->thumbnail,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('is_feature',$this->is_feature);
		$criteria->compare('sorter_category',$this->sorter_category);
		$criteria->compare('desc',$this->desc,true);
		$criteria->compare('date_created',$this->date_created,true);
		$criteria->compare('date_edited',$this->date_edited,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}